<?php
class admin_model_operate extends admin_model_base {
	public function __construct(){
		parent::__construct();
		$this->table = "operate_log";
	}
	
	/**
	 * 添加管理员操作记录
	 * Enter description here ...
	 * @param int|array|string	$res_id	资源id 可以为数组或者单个或者以","分割的字符串
	 * @param int $res_type
	 * @param int $oper
	 * @param int $uid 管理员的uid  可选
	 * @param char $username 管理员名称 可选
	 */
	public function add_operate_log($res_id,$res_type,$oper,$uid='',$username=''){
		if(!$res_id || !$res_type || !$oper) return false;
		$res_id = is_array($res_id)?$res_id:(array)$res_id;
		$sql = ' INSERT INTO '.$this->table.' VALUES  ';
		$value = $sep = '';
		!$uid && $uid= AdminController::$login['uid'];
		!$username && $username=AdminController::$login['nickname'];
		$time = date('Y-m-d H:i:s');
		foreach ($res_id as $rid){
			$value.=$sep." (NULL, $uid,'{$username}',$oper,$rid,$res_type,'$time')";
			$sep=',';
		}
		$sql.=$value;
		if($this->admin_db->query($sql)){
			$this->add_oprlog_stat($res_type, $oper, count($res_id));
			return true;
		}
		return false;
	}
	
		
	/**
	 * 更新统计表
	 * Enter description here ...
	 * @param unknown_type $res_type
	 * @param unknown_type $oper
	 * @param unknown_type $num
	 */
	public function add_oprlog_stat($res_type,$oper,$num=1){
		$uid= AdminController::$login['uid'];
		$username=AdminController::$login['nickname'];
		$date = date('Y-m-d H:i:s');
		!$num && $num=1;
		$sql = ' INSERT INTO operate_log_statistics VALUES  ('.$uid.',\''.$username.'\','.$oper.','.$res_type.','.$num.',\''.$date.'\') ON DUPLICATE KEY UPDATE opr_num=opr_num+'.$num;
		return  $this->admin_db->query($sql);
	}
	
	/**
	 * 获取操作记录表
	 * Enter description here ...
	 * @param unknown_type $args
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getOperateLogList($args = array('uid'=>'','res_type'=>'','operater'=>'','starttime'=>'','endtime'=>'','res_id'=>''),$page,$pageSize){
		$sql = 'SELECT * FROM '.$this->table.' ';
		$where = '';
		if($args['uid']) $where.=' uid='.$args['uid'];										//按uid查找						
		if($args['res_type']) $where.=($where?' AND ':' ').' res_type='.$args['res_type'];	//按类型查询
		if($args['operater']) $where.=($where?' AND ':' ').' oid='.$args['operater'];		//按操作类型查询
		if($args['starttime']) $where.=($where?' AND ':' ').' oper_time>=\''.$args['starttime'].'\'';//开始时间	
		if($args['endtime']) $where.=($where?' AND ':' ').' oper_time<=\''.$args['endtime'].'\'';//接受时间
		if($args['res_id']) $where.=($where?' AND ':' ').' res_id='.$args['res_id'];		//按资源类型
		$sql.=$where?' WHERE '.$where:'';
		return $this->admin_db->selectPage($sql,array(),$page,$pageSize);
	}
	
/**
	 * 获取操作记录统计信息
	 * Enter description here ...
	 * @param unknown_type $args
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getOperateLogStat($args = array('uid'=>'','res_type'=>'','operater'=>'','starttime'=>'','endtime'=>'','min'=>'','max'=>''),$page=1,$pageSize=20,&$total){
		$sql = 'SELECT * FROM operate_log_statistics  ';
		$where = '';
		if($args['uid']) $where.=' uid='.$args['uid'];										//按uid查找						
		if($args['res_type']) $where.=($where?' AND ':' ').' res_type='.$args['res_type'];	//按类型查询
		if($args['operater']) $where.=($where?' AND ':' ').' opr_type='.$args['operater'];		//按操作类型查询
		if($args['starttime']) $where.=($where?' AND ':' ').' opr_date>=\''.$args['starttime'].'\'';//开始时间	
		if($args['endtime']) $where.=($where?' AND ':' ').' opr_date<=\''.$args['endtime'].'\'';//接受时间
		if($args['min']) $where.=($where?' AND ':' ').' opr_num>='.$args['min'];		//按资源类型
		if($args['max']) $where.=($where?' AND ':' ').' opr_num<='.$args['max'];		//按资源类型
		$sql.=$where?' WHERE '.$where:'';
		$total_sql  = 'SELECT SUM(opr_num) AS total FROM operate_log_statistics ';
		$total =$this->admin_db->fetchField($total_sql.($where?' WHERE '.$where:''), 'total');
		return $this->admin_db->selectPage($sql,array(),$page,$pageSize);
	}
}